import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_youren/pages/AppContants.dart';
import 'package:flutter_youren/pages/main/HomeWidget.dart';
import 'package:flutter_youren/route.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_youren/util/LogUtils.dart';
import 'package:flutter_youren/util/MMKVUtil.dart';
import 'package:flutter_youren/util/RoutUtil.dart';
import 'package:mmkv/mmkv.dart';
import 'DrawerListItem.dart';
import 'package:flutter/rendering.dart';

Future<void> main() async {
  final rootDir = await MMKV.initialize();
  debugPaintSizeEnabled = false;
  print('MMKV for flutter with rootDir = $rootDir');
  var map = await RoutUtil.instance.getPageName();
  String pateName = (map["startFlutterPageName"] ?? MYHOME_PAGE) as String;
  int nativeNum = (map["startNativePageNum"] ?? 1) as int;
  MMKVUtil.instance.putInt("startNativePageNum", nativeNum);
  runApp(MyApp(pateName));
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
      statusBarColor: Colors.transparent, //设置为透明
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

class MyApp extends StatelessWidget {
  final String? pageName;

  const MyApp(this.pageName, {super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    LogUtils.d("main", "onGenerateRoute pageName ${pageName}");
    return MaterialApp(
      title: 'Flutter Demo',
      localizationsDelegates: AppLocalizations.localizationsDelegates,
      supportedLocales: AppLocalizations.supportedLocales,
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      //如果在这里注册里钩子里面不会生效
      routes: RouteConfiguration.routes(),
      onGenerateRoute: RouteConfiguration.onGenerateRoute,
      initialRoute: pageName,//app启动的时候进入的页面，如果home有值就会把home压到栈底再显示该页面，如果没有home就直接显示VideoPage页面。
      onUnknownRoute: RouteConfiguration.onUnknownRoute,
      //如果上面用到了onGenerateRoute这个钩子一般就可以在里面处理路由不到的页面问了
      //home: const MyHomePage(title: 'home'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _currentIndex = 0; //导航栏底部按钮
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    final local = AppLocalizations.of(context)!;
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: IndexedStack(
        index: _currentIndex,
        children: [
          const HomeWidget(),
          Container(color: Colors.green),
          Container(color: Colors.yellow),
          Container(color: Colors.blue),
        ],
      ),
      drawer: Drawer(
        child: Container(
          child: ListView(
            children: [
              UserAccountsDrawerHeader(
                onDetailsPressed: () {
                  print('onDetailsPressed');
                },
                arrowColor: Colors.red,
                accountName: const Text("红红",
                    style: TextStyle(color: Colors.white, fontSize: 15)),
                accountEmail: const Text("1096877329@qq.com",
                    style: TextStyle(color: Colors.white, fontSize: 15)),
                //账户头像
                currentAccountPicture: InkWell(
                    onTap: () {
                      Navigator.pushNamed(context, "/AccountDetailPage");
                    },
                    child: const CircleAvatar(
                      backgroundImage: NetworkImage(
                          "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic_source%2F59%2F97%2Fce%2F5997ce5852a7feff8cef2b44c327aa4e.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1659003012&t=b078c2463adbd51570e8afae4171e4fe"),
                    )),
                //配置背景
                decoration: const BoxDecoration(
                    color: Colors.grey,
                    image: DecorationImage(
                        image: NetworkImage(
                            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1115%2F101021113337%2F211010113337-2-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1659010104&t=6a975b1fa54fa39612a52acc18f541bf"),
                        fit: BoxFit.cover)),
                //配置其他
                otherAccountsPictures: const <Widget>[
                  Text("其他内容",
                      style: TextStyle(color: Colors.lightBlue, fontSize: 10))
                ],
              ),
              DrawerListItem(
                  icons: Icons.video_collection, title: local.video, index: 0),
              DrawerListItem(icons: Icons.image, title: local.image, index: 1),
              DrawerListItem(
                  icons: Icons.music_note, title: local.music, index: 2),
              DrawerListItem(icons: Icons.share, title: local.shared, index: 3),
              DrawerListItem(
                  icons: Icons.settings, title: local.setting, index: 4),
            ],
          ),
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(label: '首页', icon: Icon(Icons.widgets)),
          BottomNavigationBarItem(label: '时间线', icon: Icon(Icons.timeline)),
          BottomNavigationBarItem(label: '统计', icon: Icon(Icons.equalizer)),
          BottomNavigationBarItem(label: '我的', icon: Icon(Icons.perm_identity)),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
